package com.istudy.servlet;

import com.istudy.entity.Comment;
import com.istudy.entity.Message;
import com.istudy.entity.User;
import com.istudy.service.imp.VideoServiceImpl;
import com.istudy.service.interfaces.CommentService;
import com.istudy.service.imp.CommentServiceImpl;
import com.istudy.service.interfaces.VideoService;
import com.istudy.utils.ReturnResult;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/Comment"},name = "comment")
public class CommentServlet extends AbstractServlet {
    public CommentService commentService = new CommentServiceImpl();
    VideoService videoService=new VideoServiceImpl();
    /**
     * 获取servlet类对象
     *
     * @return
     */
    @Override
    public Class getServletClass() {
        return CommentServlet.class;
    }

    @Override
    public void init() throws ServletException {
        super.init();

        commentService = new CommentServiceImpl();
    }


    /**
     * 条件查询控制器
     * @param
     * @param response
     */
    public String getCommentByUser(HttpServletRequest request, HttpServletResponse response){
        if(request.getSession().getAttribute("user")!=null){
            int num=1;
            if(request.getParameter("currentPage")!=null) {
                num = Integer.parseInt(request.getParameter("currentPage"));
            }
        List<Comment> partComment = commentService.getCommentByUser(request,num);
        request.setAttribute("commentList",partComment);
        return  "mycomment";
        }else {
            return  "login";
    }
    }
    /**
     * 条件查询控制器
     * @param
     * @param response
     */
    public void getCommentByVideo(HttpServletRequest request, HttpServletResponse response){
        List<Object> objects=new ArrayList<>();
        int num=1;
        if(request.getParameter("currentPage")!=null) {
            num = Integer.parseInt(request.getParameter("currentPage"));
        }
        List<Comment> partComment = commentService.getCommentByVideo(request,num);
        request.setAttribute("commentsPage",partComment); // request中添加评论分页,;

    }
    /**
     * 条件查询控制器
     * @param
     * @param response
     */
    public List fanyeComment(HttpServletRequest request, HttpServletResponse response){
        List<Object> objects=new ArrayList<>();
        int num=1;
        if(request.getParameter("currentPage")!=null) {
            num = Integer.parseInt(request.getParameter("currentPage"));
        }
        int id = Integer.parseInt(request.getParameter("videoId"));
        request.setAttribute("video",videoService.selectVideoById(id));
        List<Comment> partComment = commentService.getCommentByVideo(request,num);
        return  partComment;
    }
    /**
     * 新增评论控制器
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    public Message insertComment(HttpServletRequest request, HttpServletResponse response) throws ParseException {
         //实体类映射
        Comment comment=new Comment();
        comment.setCommentUserId(Integer.parseInt(request.getParameter("userid")));
        comment.setCommentVideoId(Integer.parseInt(request.getParameter("videoId")));
        comment.setCommentContent(request.getParameter("commentContent"));
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //获取评论时间
        Date date=new Date();
        comment.setCommentDataTime(sf.format(date));
        boolean b = commentService.insertComment(comment);
        Message message = new Message();
        if(b){
            message.setCode(1);
            message.setMessage("发表成功");
        } else{
            message.setCode(0);
            message.setMessage("发表失败");
        }
        return message;
    }
    public ReturnResult delComment(HttpServletRequest request, HttpServletResponse response){
        boolean i = commentService.delComment(request);
        ReturnResult returnResult = new ReturnResult();
        if(i){
            returnResult.setStatus(1);
            returnResult.setMessage("删除成功");
        } else  {

            returnResult.setStatus(-1);
            returnResult.setMessage("删除失败");
        }
        return returnResult;

    }
}
